package action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import proxy.ProxyFactory;
import service.StatisticsService;
import service.StatisticsServiceInterface;
import utils.CalculateDate;
import utils.MakeExcelHelper;
import utils.PageBean;

import beans.Agent;
import beans.DataCompany;
import beans.DataStatistics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class StatisticsAction extends ActionSupport {

	private int curpage;//当前页
	private int pageSize;//页大小
	private int branchID;//设备网点ID
	private int terminalID;//设备ID
	private String flag;
	private int day;
	
	private Timestamp startstime = null; // 开始时间
	private Timestamp endtime = null; // 结束时间

	private static DataStatistics basicData = null;

	private static PageBean<DataStatistics> branchData = null;
	private PageBean<DataStatistics> terminalData = null;
	private PageBean<DataCompany> companyData = null;
	private PageBean<?> commonlyUsers = null;
	
	StatisticsServiceInterface statisticsService = (StatisticsServiceInterface)ProxyFactory
			.getProxyInstance(new StatisticsService());
	
	ActionContext actionContext = ActionContext.getContext();
	Map<String, Object> session = actionContext.getSession();
	Agent agent = (Agent) session.get("admin");
	
	/**
	 * 获取常用用户列表
	 * @return
	 */
	public String commonlyUseduser(){
		commonlyUsers = statisticsService.queryDistributionData(agent.getId(), curpage, pageSize);
		if("list".equals(flag)){
			return "commonlyUsers";
		}else{
			return "commonlyUsersAjax";
		}
	}
	
	/**
	 * 导出快递网点统计数据
	 * @throws Exception
	 * by zgy
	 */
	public void getExcel() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		MakeExcelHelper.MakeExcelByBranchData(branchData.getList(),basicData.getsTime(),basicData.geteTime(), response);
	}
	
	/**
	 * 数据统计查询页
	 * @return
	 * by zgy
	 */
	public String statisticsSearch(){
		flag="list";
		curpage=1;
		pageSize=10;
		if(branchID==-1){
			return branchData();
		}else if(branchID!=-1&&terminalID==-1){
			return branchDataDetails();
		}else{
			return terminalDataDetails();
		}
	}

	/**
	 * 获取基础数据
	 * 
	 * @return
	 */
	public String basicData() {
		basicData = statisticsService.getBasicData(agent.getId(),startstime,endtime,"agent");
		return "basicData";
	}
	
	/**
	 * 获取详细数据
	 * 
	 * @return
	 */
	public String branchData() {
		if (pageSize == 0) {
			pageSize = 1;
		}
		Date now = new Date();
		if("list".equals(flag)){
			basicData = statisticsService.getBasicData(agent.getId(),startstime,endtime,"agent");
			branchData = statisticsService.getDetailData(agent.getId(), curpage, pageSize,startstime,endtime,"branch");
			return "agentData";
		}else if("time".equals(flag)){
			basicData = statisticsService.getBasicData(agent.getId(),startstime,endtime,"agent");
			branchData = statisticsService.getDetailData(agent.getId(), curpage, pageSize,startstime,endtime,"branch");
			return "dataOfTime";
		}else if("monthData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 30).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(agent.getId(),startstime,endtime,"agent");
			branchData = statisticsService.getDetailData(agent.getId(), curpage, pageSize,startstime,endtime,"branch");
			return "dataOfTime";
		}else if("weekData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 7).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(agent.getId(),startstime,endtime,"agent");
			branchData = statisticsService.getDetailData(agent.getId(), curpage, pageSize,startstime,endtime,"branch");
			return "dataOfTime";
		}else if("dayData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 0).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(agent.getId(),startstime,endtime,"agent");
			branchData = statisticsService.getDetailData(agent.getId(), curpage, pageSize,startstime,endtime,"branch");
			return "dataOfTime";
		}else{
			branchData = statisticsService.getDetailData(agent.getId(), curpage, pageSize,startstime,endtime,"branch");
			return "agentDataAjax";
		}
	}
	/**
	 * 网点数据详情
	 * @return
	 */
	public String branchDataDetails() {
		if (pageSize == 0) {
			pageSize = 1;
		}
		Date now = new Date();
		if("list".equals(flag)){
			basicData = statisticsService.getBasicData(branchID,startstime,endtime,"branch");
			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "branchData";
		}else if("time".equals(flag)){
			basicData = statisticsService.getBasicData(branchID,startstime,endtime,"branch");
			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "branchDataOfTime";
		}else if("monthData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 30).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(branchID,startstime,endtime,"branch");
			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "branchDataOfTime";
		}else if("weekData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 7).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(branchID,startstime,endtime,"branch");
			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "branchDataOfTime";
		}else if("dayData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 0).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(branchID,startstime,endtime,"branch");
			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "branchDataOfTime";
		}else{
			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "branchDataAjax";
		}
	}
	/**
	 * 设备数据详情
	 * @return
	 */
	public String terminalDataDetails() {
		Date now = new Date();
		if("list".equals(flag)){
			basicData = statisticsService.getBasicData(terminalID,startstime,endtime,"terminal");
//			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "terminalData";
		}else if("time".equals(flag)){
			basicData = statisticsService.getBasicData(terminalID,startstime,endtime,"terminal");
//			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "terminalDataOfTime";
		}else if("monthData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 30).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(terminalID,startstime,endtime,"terminal");
//			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "terminalDataOfTime";
		}else if("weekData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 7).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(terminalID,startstime,endtime,"terminal");
//			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "terminalDataOfTime";
		}else if("dayData".equals(flag)){
			startstime = new Timestamp(CalculateDate.getDateBeforeByDay(now, 0).getTime());
			endtime = new Timestamp(now.getTime());
			basicData = statisticsService.getBasicData(terminalID,startstime,endtime,"terminal");
//			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "terminalDataOfTime";
		}else{
//			terminalData = statisticsService.getDetailData(branchID, curpage, pageSize,startstime,endtime,"terminal");
			return "terminalDataAjax";
		}
	}
	/**
	 * 获取快递公司存件数
	 * @return
	 * @throws Exception 
	 */
	public String agentCompanyData(){
		if (pageSize == 0) {
			pageSize = 1;
		}
		companyData = statisticsService.getCompanyData(agent.getId(), curpage, pageSize,startstime, endtime,"agent");
		if("list".equals(flag)){
			return "agentCompanyData";
		}else{
			return "agentCompanyDataAjax";
		}
	}
	/**
	 * 获取网点快递公司数据
	 * @return
	 */
	public String branchCompanyData(){
		if (pageSize == 0) {
			pageSize = 1;
		}
		companyData = statisticsService.getCompanyData(branchID, curpage, pageSize,startstime, endtime,"branch");
		if("list".equals(flag)){
			return "branchCompanyData";
		}else{
			return "branchCompanyDataAjax";
		}
	}
	/**
	 * 设备快递公司数据统计
	 * @return
	 */
	public String terminalCompanyData(){
		if (pageSize == 0) {
			pageSize = 1;
		}
		companyData = statisticsService.getCompanyData(terminalID, curpage, pageSize,startstime, endtime,"terminal");
		if("list".equals(flag)){
			return "terminalCompanyData";
		}else{
			return "terminalCompanyDataAjax";
		}
	}
	/**
	 * 获取快递公司使用率统计图片
	 * @return
	 * @throws Exception 
	 */
	/*public String getCompanyIMG() throws Exception{
		chart = JFreeChartHelper.createBarChart(companyData.getList());
		return "companyImg";
	}*/
	/**
	 * 页面跳转
	 * @return
	 */
	/*public String returnJSP(){
		return "pageIMG";
	}*/
	public String returnSearch(){
		return "tatisticsSearch";
	}

	public int getCurpage() {
		return curpage;
	}

	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getBranchID() {
		return branchID;
	}

	public void setBranchID(int branchID) {
		this.branchID = branchID;
	}

	public int getTerminalID() {
		return terminalID;
	}

	public void setTerminalID(int terminalID) {
		this.terminalID = terminalID;
	}

	public void setBasicData(DataStatistics basicData) {
		StatisticsAction.basicData = basicData;
	}

	public DataStatistics getBasicData() {
		return basicData;
	}

	public PageBean<DataStatistics> getBranchData() {
		return branchData;
	}

	public void setBranchData(PageBean<DataStatistics> branchData) {
		StatisticsAction.branchData = branchData;
	}

	public PageBean<DataStatistics> getTerminalData() {
		return terminalData;
	}

	public void setTerminalData(PageBean<DataStatistics> terminalData) {
		this.terminalData = terminalData;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Timestamp getStartstime() {
		return startstime;
	}

	public void setStartstime(Timestamp startstime) {
		this.startstime = startstime;
	}

	public Timestamp getEndtime() {
		return endtime;
	}

	public void setEndtime(Timestamp endtime) {
		this.endtime = endtime;
	}

	public PageBean<DataCompany> getCompanyData() {
		return companyData;
	}

	public void setCompanyData(PageBean<DataCompany> companyData) {
		this.companyData = companyData;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public PageBean<?> getCommonlyUsers() {
		return commonlyUsers;
	}

	public void setCommonlyUsers(PageBean<?> commonlyUsers) {
		this.commonlyUsers = commonlyUsers;
	}
	
}
